<?php
/**
 * Created by PhpStorm.
 * User: i5_131009a
 * Date: 2015/12/17
 * Time: 17:13
 */
require_once GM_LIBRARY."/PHPExcel/IOFactory.php";
class ExcelRead {
    private $excel = null;
    private $worksheet = null;
    protected $columnNameRow = null;
    protected $dataStartRow = null;
    protected $displayNameRow = null;
    protected $sheetIndex = null;
    protected $sheetHighestColumn = null;
    protected $sheetHighestRow = null;
    protected $columnNamePostion = null;
    protected $displatName = null;
    protected $maxRows = null;


    function __construct()
    {

    }

    function initialize($excelPath, $sheetIndex, $columnNameRow, $dataStartRow, $displayNameRow = null)
    {
        $this->excel = PHPExcel_IOFactory::load($excelPath);
        if(is_string($sheetIndex)){
            $this->worksheet = $this->excel->getSheetByName($sheetIndex);
        }else{
            $this->worksheet = $this->excel->getSheet($sheetIndex);
        }

        $this->columnNameRow = $columnNameRow;
        $this->dataStartRow = $dataStartRow;
        $this->sheetIndex = $sheetIndex;
        $this->displayNameRow = $displayNameRow;
        $this->sheetHighestColumn = $this->worksheet->getHighestColumn();
        $this->sheetHighestRow = $this->worksheet->getHighestRow();
        $this->maxRows = (int)($this->sheetHighestRow) - (int)($this->dataStartRow);
        $this->setColumnNamePostion();
        if(count($this->columnNamePostion) == 0)
        {
            return false;
        }
        return true;
    }

    function getHighestRow()
    {
        return $this->sheetHighestRow;
    }

    function setColumnNamePostion()
    {
        $maxColumnIndex = PHPExcel_Cell::columnIndexFromString($this->sheetHighestColumn);
        for($i = 0; $i <= $maxColumnIndex;$i++)
        {
            $columnNamePostion = PHPExcel_Cell::stringFromColumnIndex($i).$this->columnNameRow;
            $colName = $this->worksheet->getCell($columnNamePostion)->getValue();
            $colName = trim($colName);
            if(!is_null($colName) && $colName != "")
            {
                if(!is_null($this->displayNameRow))
                {
                    $displayNamePostion = PHPExcel_Cell::stringFromColumnIndex($i).$this->displayNameRow;
                    $this->displatName[$colName] = $this->worksheet->getCell($displayNamePostion)->getValue();
                }
                $this->columnNamePostion[$colName] = PHPExcel_Cell::stringFromColumnIndex($i);
            }else{
                break;
            }
        }
    }

    function getColumnNameList()
    {
        return array_keys($this->columnNamePostion);
    }

    public function getOneRowData($row)
    {
        $data = array();
        if($row > ($this->maxRows)){
            return false;
        }
        foreach ($this->columnNamePostion as $columnName => $columnPostion) {
            $dataRow = $this->dataStartRow + $row;
            $data[$columnName] = $this->worksheet->getCell($columnPostion.$dataRow)->getCalculatedValue();
            if(is_object($data[$columnName]))
            {
                $data[$columnName] = $data["$columnName"]->getPlainText();
            }
//            if(substr($data[$columnName],0,1) == "="){
//                $data[$columnName] = $this->worksheet->getCell($columnPostion.$dataRow)->getCalculatedValue();
//            }
        }
        if(reset($data) == ""){
            $this->maxRows = $row;
            return false;
        }

        return $data;
    }

}